//
//  YXBottomButtonView.m
//  MockProject
//
//  Created by FanYu on 24/8/2016.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXBottomButtonView.h"

@interface YXBottomButtonView ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *separatorlineView;
@property (nonatomic, strong) UIView *hairlineView;
@property (nonatomic, strong) UIButton *selectAllButton;
@property (nonatomic, strong) UIButton *deleteButton;

@end


@implementation YXBottomButtonView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Setup UI
- (void)setupUI {
    // content view
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor colorWithHexString:@"fcfcfc"];
    [self addSubview:self.contentView];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    // hairline view
    self.hairlineView = [[UIView alloc] init];
    self.hairlineView.backgroundColor = [UIColor blackColor];
    self.hairlineView.alpha = 0.2;
    [self.contentView addSubview:self.hairlineView];
    
    [self.hairlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.width.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    // separator view
    self.separatorlineView = [[UIView alloc] init];
    self.separatorlineView.backgroundColor = [UIColor colorWithHexString:@"e2e2e4"];
    [self.contentView addSubview:self.separatorlineView];
    
    [self.separatorlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(0.5, 20));
        make.center.equalTo(self);
    }];
    
    // selectAll button
    self.selectAllButton = [[UIButton alloc] init];
    self.selectAllButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.selectAllButton setTitle:@"全选" forState:UIControlStateNormal];
    [self.selectAllButton setTitleColor:[UIColor colorWithHexString:@"403e3d"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.selectAllButton];
    
    [self.selectAllButton addTarget:self action:@selector(selectAllButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.selectAllButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.contentView.mas_width).multipliedBy(0.5);
        make.left.equalTo(self.contentView.mas_left);
        make.top.equalTo(self.contentView.mas_top);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    
    // delete button
    self.deleteButton = [[UIButton alloc] init];
    self.deleteButton.enabled = NO;
    self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [self.deleteButton setTitleColor:[UIColor colorWithHexString:@"cccccc"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.deleteButton];
    
    [self.deleteButton addTarget:self action:@selector(deleteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.contentView.mas_width).multipliedBy(0.5);
        make.left.equalTo(self.selectAllButton.mas_right);
        make.top.equalTo(self.contentView.mas_top);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
}


#pragma mark - Actions 
- (void)selectAllButtonTapped:(UIButton *)sender {
    [self.delegate selectAllButtonTapped:sender];
}

- (void)deleteButtonTapped:(UIButton *)sender {
    [self.delegate deleteButtonTapped:sender];
}


#pragma mark - Helper 
- (void)updateSelectedAmount:(NSInteger)amount {
    if (amount == 0) {
        [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [self.deleteButton setTitleColor:[UIColor colorWithHexString:@"cccccc"] forState:UIControlStateNormal];
        self.deleteButton.enabled = NO;
    } else {
        [self.deleteButton setTitle:[NSString stringWithFormat:@"删除(%ld)", amount] forState:UIControlStateNormal];
        [self.deleteButton setTitleColor:[UIColor colorWithHexString:@"ff4600"] forState:UIControlStateNormal];
        self.deleteButton.enabled = YES;
    }
}

@end
